#include "Platform.h"

void Platform::addGoingUpStair(Stair *up) {
	goingUp.push_back(up);
}

void Platform::addGoingDownStair(Stair *down) {
	goingDown.push_back(down);
}

void Platform::addSpinachBox(SpinachBox *box) {
	spinachBoxList.push_back(box);
}

void Platform::AlignWithPlatform(Sprite *s) {
	s->SetY(this->rect.y - s->GetRect().h);
}

Stair* Platform::CollidesWithGoingUpStair(Sprite* s) {
	int x1, y1, x2, y2, x3, y3, x4, y4;
	Rect b1, *b2;
	b1 = s->GetRect();
	x1 = s->GetX();
	y1 = s->GetY();
	x2 = x1+b1.w;
	y2 = y1+b1.h;
	std::list<Stair*>::iterator i = goingUp.begin();
	while(i != goingUp.end()) {
		b2 = (*i)->GetBottomStep()->GetRect();
		//check for collision
		x3 = b2->x;
		y3 = b2->y;
		x4 = x3+b2->w/2;
		y4 = y3+b2->h;
		//printf("[(%d,%d),(%d,%d)], [(%d,%d),(%d,%d)]\n", x1, y1, x2, y2, x3, y3, x4, y4);
		if((!(x2 < x3) && !(x1 > x4)) && (!(y2 < y3) && !(y1 > y4))) {
			return *i;
		}
		i++;
	}
	return NULL;
}

Stair* Platform::CollidesWithGoingDownStair(Sprite* s) {
	int x1, y1, x2, y2, x3, y3, x4, y4;
	Rect b1, *b2;
	b1 = s->GetRect();
	x1 = s->GetX();
	y1 = s->GetY();
	x2 = x1+b1.w;
	y2 = y1+b1.h;
	std::list<Stair*>::iterator i = goingDown.begin();
	while(i != goingDown.end()) {
		b2 = (*i)->GetTopStep()->GetRect();
		//check for collision
		x3 = b2->x;
		y3 = b2->y-5; //need to adjust y, because it is actually bellow platform level
		x4 = x3+b2->w/2;
		y4 = y3+b2->h;
		//printf("[(%d,%d),(%d,%d)], [(%d,%d),(%d,%d)]\n", x1, y1, x2, y2, x3, y3, x4, y4);
		if((!(x2 < x3) && !(x1 > x4)) && (!(y2 < y3) && !(y1 > y4))) {
			return *i;
		}
		i++;
	}
	return NULL;
}

SpinachBox *Platform::CollidesWithSpinachBox(Sprite *s, int dx) {
	int x1, y1, x2, y2, x3, y3, x4, y4;
	Rect b1, b2;
	b1 = s->GetRect();
	x1 = s->GetX()+dx;
	y1 = s->GetY();
	x2 = x1+b1.w;
	y2 = y1+b1.h;
	if(spinachBoxList.empty()) return NULL;
	std::list<SpinachBox*>::iterator i = spinachBoxList.begin();
	while(i != spinachBoxList.end()) {
		b2 = (*i)->GetRect();
		//check for collision
		x3 = b2.x;
		y3 = b2.y;
		x4 = x3+b2.w;
		y4 = y3+b2.h;
		//printf("[(%d,%d),(%d,%d)], [(%d,%d),(%d,%d)]\n", x1, y1, x2, y2, x3, y3, x4, y4);
		if((!(x2 < x3) && !(x1 > x4)) && (!(y2 < y3) && !(y1 > y4))) {
			return (*i);
		}
		i++;
	}
	return NULL;
}

bool Platform::CollidesWithEdge(Sprite *s, int dx) {
	int x1, y1, x2, y2;//, x3, y3, x4 ,y4;
	Rect b1, b2;
	b1 = s->GetRect();
	x1 = s->GetX()+dx;
	y1 = s->GetY();
	x2 = x1+b1.w;
	y2 = y1+b1.h;
	Rect left_edge(0,0,0,0), right_edge(0,0,0,0);
	left_edge.x = this->GetRect().x;
	right_edge.x = this->GetRect().x+this->GetRect().w;
	if(!left_open) {
		if(x1 <= left_edge.x) {
			s->SetX(left_edge.x);
			return true;
		}
	}
	if(!right_open) {
		if(x2 >= right_edge.x) {
			s->SetX(right_edge.x-s->GetRect().w);
			return true;
		}
	}
	return false;
}

bool Platform::CollidesWithSea(Sprite *s) {
	if(this->GetRect().y == 408) {
		if(s->GetRect().y >= 380) {
			return true;
		}
		else 
			return false;
	}
	
	return false;
}